// Created by Duncan Gunn.  This is Open Source Software.
//
// Please be kind and be constructive with any comments, I've
// never attempted to put a project on a repository before
// and also freely admit this program is somewhat thin on comments
//

#INCLUDE <16F819.H>
#FUSES INTRC, NOWDT, PUT, MCLR, NOBROWNOUT
#USE DELAY (CLOCK = 8000000)

#include "Hardware.h"
#include "isr_handler.h"
#include "TrigStateMachine.h"
#include "TIM_Memory.h"
#include "TIM_Adjust_SM.h"

char curr_mem;

//
// Variables for the encoder
//
EncoderStateStruct wheel;


int main( void )
{
// Initialise system
//
	SetUnusedPins();
	// Reset the display by clearing the 74LS595
	//
	reset595();
	InitEncoderStateStruct( &wheel );
	ServiceInterruptInit();
	Set_Rel(0);

	curr_mem = read_eeprom(16);

	FlashMode(timing);

// Main loop.  Entry into timer run mode.
	while(1)
	{
		BarPoint(curr_mem);

		// Trigger handler for Timing Mode
		//
		if( TrigState() ) 
		{
			Trigger_Load = TIM_Fetch(curr_mem);
			SM_Tim_main();
		}
		else
			Set_Rel(0);

		// Handler for the Program Selection Mode
		//
		if(MultiPushButton() == on)
		{
			// Handle new mode
			//
			curr_mem = TIM_ChangeProg(curr_mem);

			// Store result
			//			
			write_eeprom(16,curr_mem);
			Delay_Ticks( EE_LATENCY );

			// Restate mode now we've re-entered it.
			//
			FlashMode(timing);
		}	
	}
}